package com.szty.libarytest.mediaProjection

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import com.szty.libarytest.R
import kotlinx.android.synthetic.main.activity_timer_test.*

class TimerTestActivity : AppCompatActivity() {
    var isCountTime = false
    var time = 0
    private val mHandler = Handler(Looper.myLooper()!!, Handler.Callback {
        when(it.what){
            1->{
                time ++
                tv_time.text = time.toString()
            }
        }
        return@Callback true
    })

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_timer_test)
        btn_start_time.setOnClickListener {
            if(isCountTime)return@setOnClickListener
            isCountTime = true
            Thread{
                while (isCountTime){
                    val obtain = Message.obtain()
                    obtain.what = 1
                    mHandler.sendMessage(obtain)
                    Thread.sleep(1000)
                }
            }.start()
        }
        btn_stop_time.setOnClickListener {
            isCountTime = false
        }

    }
}